package com.android.vestel.photoroid;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.util.Log;

public abstract class WebPhotos {
	String TAG = "WebPhotos";

	boolean downloadFile(String url, File outputFile) {
		int contentLength = 0;
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			contentLength = conn.getContentLength();

			if (contentLength != 0) {
				DataInputStream stream = new DataInputStream(u.openStream());

				byte[] buffer = new byte[contentLength];
				stream.readFully(buffer);
				stream.close();

				DataOutputStream fos = new DataOutputStream(
						new FileOutputStream(outputFile));
				fos.write(buffer);
				fos.flush();
				fos.close();
			} else {
				Log.e(TAG, "The file's size is 0.");
			}
		} catch (FileNotFoundException e) {
			return false; // swallow a 404
		} catch (IOException e) {
			return false; // swallow a 404
		}
		if (contentLength == outputFile.length()) {
			Log.e(TAG, outputFile.getAbsolutePath()
					+ " Downloaded Successfully");
			return true;
		} else {
			Log.e(TAG,
					"Downloaded file size is not consistent with the file on server");
			return false;
		}
	}

	abstract public ArrayList<String> getImagePaths();

	abstract public ArrayList<String> getImageIDs();
}
